define('HashApp', null, function() {

// router.add(['/index'], () => {})
function HashApp() {
    this.redirectWhenEmpty = '';
    this.routes = [];

    this.init();
}
HashApp.prototype = {
    constructor: HashApp,
    addChar: function(str) {
        if('' === str) {
            return str;
        }

        if('/' !== str.charAt(0)) {
            return '/' + str;
        }

        return str;
    },
    init: function() {
        var that = this;
        window.onhashchange = function() {
            that.run();
        };
    },
    add: function(routes, handle) {
        var ret = [];
        for(var i=0, len=routes.length; i<len; i++) {
            ret.push(this.addChar(routes[i]));
        }

        var bin = {
            r: ret,
            h: handle
        };
        this.routes.push(bin);
    },
    run: function() {
        // /index
        var pathname = window.location.hash.replace('#', '');

        pathname = this.addChar(pathname);
        if('' === pathname) {
            pathname = this.addChar(this.redirectWhenEmpty);
        }
        if('' === pathname) {
            return;
        }

        // ['/', post', 'list']
        var com = pathname.split('/');
        var lastPath = '';
        for(var i=0; i<com.length; i++) {
            lastPath = lastPath + '/' + com[i];
            lastPath = lastPath.replace('//', '/');

            this.renderComponent(lastPath);
        }
    },
    renderComponent: function(path) {
        for(var i=0, len=this.routes.length; i<len; i++) {
            var bin = this.routes[i];
            var handled = false;

            for(var x=0, length=bin.r.length; x<length; x++) {
                if(path === bin.r[x]) {
                    bin.h();

                    handled = true;
                    break;
                }
            }

            if(handled) {
                break;
            }
        }
    }
};

return HashApp;
});
